package com.ay277.htqoa.shiro.shiroToken;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;

/**
 * Created by Me on 2016-12-24.
 */
public class shiro1 {
    public static void main(String[] args) throws Exception {
        //读取配置文件，初始化SecurityManager 工厂  引用简单配置文件 shiro.ini
        Factory<SecurityManager> securityManagerFactory=new IniSecurityManagerFactory("classpath:shiro.ini");
        //获取SercurityManager实例
        SecurityManager securityManager=securityManagerFactory.getInstance();
        //将SecurityManager 绑定到SecurityUtils上
        SecurityUtils.setSecurityManager(securityManager);
        //获取当前用户
        Subject currentUser=SecurityUtils.getSubject();
        //UsernamePasswordToken 最简单的shiro 验证  令牌
        UsernamePasswordToken token=new UsernamePasswordToken("admin","admin");
        try {
            //Suject 当前用户登陆
            currentUser.login(token);
            System.out.println("登陆成功！");
        }catch (AuthenticationException e){
            e.printStackTrace();
            System.out.println("登陆失败！");
        }
        //退出
        currentUser.logout();

    }
}
